package com.tax.infrastructure.statemachine.impl;

import com.tax.infrastructure.statemachine.State;
import com.tax.infrastructure.statemachine.StateMachine;
import com.tax.infrastructure.statemachine.StateMachineFactory;
import com.tax.infrastructure.statemachine.builder.ExternalTransitionBuilder;
import com.tax.infrastructure.statemachine.builder.StateMachineBuilder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * StateMachineBuilderImpl
 *
 * @param <S>
 * @param <E>
 * @param <C>
 */
public class StateMachineBuilderImpl<S, E, C> implements StateMachineBuilder<S, E, C> {

    /**
     * stateMap is the same with stateMachine,as the core of state machine is holding reference to states
     */
    private final Map<S, State<S, E, C>> stateMap = new ConcurrentHashMap<>();
    private final StateMachineImpl<S,E,C> stateMachine=new StateMachineImpl<>(stateMap);

    @Override
    public ExternalTransitionBuilder<S, E, C> externalTransition() {
        return new TransitionBuilderImpl(stateMap,TransitionType.EXTERNAL);
    }

    @Override
    public StateMachine<S, E, C> build(String machineId) {
        stateMachine.setMachineId(machineId);
        stateMachine.setReady(true);
        StateMachineFactory.register(stateMachine);
        return stateMachine;
    }


}
